package com.yisu.expressway.api.web.controller;

import com.google.common.collect.Lists;
import com.yisu.expressway.api.core.service.IGoodsCategoryService;
import com.yisu.expressway.api.core.service.IModuleService;
import com.yisu.expressway.api.domain.config.SystemConfig;
import com.yisu.expressway.api.domain.request.ModuleDataRequest;
import com.yisu.expressway.api.domain.response.ModuleDataResponse;
import com.yisu.expressway.api.domain.response.RecommendGoodsResponse;
import com.yisu.expressway.api.domain.vo.CategoryBannerVo;
import com.yisu.expressway.api.domain.vo.CategoryGoodsVo;
import com.yisu.expressway.api.domain.vo.GoodsCategoryVo;
import com.yisu.expressway.api.web.util.GoodsCategoryUtil;
import com.yisu.expressway.ewf.common.web.JsonResult;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.linq4j.function.Predicate1;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 模块数据接口
 * Created by zj on 2017/4/28.
 */
@RequestMapping("/module")
@Controller
public class ModuleController extends BaseController {
    @Resource
    private IModuleService iModuleService;
    @Resource
    private SystemConfig systemConfig;
    @Resource
    private IGoodsCategoryService iGoodsCategoryService;

    /**
     * 获取模块数据
     *
     * @param token             token令牌， 可为空
     * @param moduleDataRequest
     * @return json响应
     */
    @RequestMapping(value = "/moduleData", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult moduleData(@RequestHeader(value = "X-Long-Token", required = false) String token, @RequestBody ModuleDataRequest moduleDataRequest) {
        JsonResult result = new JsonResult();
        ModuleDataResponse moduleDataResponse = new ModuleDataResponse();
        List<CategoryBannerVo> categoryBannerVos = Lists.newArrayList();
        List<GoodsCategoryVo> goodsCategoryForFirstLevelVos = Lists.newArrayList();
        List<GoodsCategoryVo> goodsCategoryForSecondLevelVos = Lists.newArrayList();
        List<CategoryGoodsVo> categoryGoodsVos = Lists.newArrayList();
        try {
            //获取分类
            goodsCategoryForFirstLevelVos= new GoodsCategoryUtil().switchModel(iGoodsCategoryService.getFirstLevelCategoryList());
            if(StringUtils.isEmpty(moduleDataRequest.getCategoryCode()))
            {
                GoodsCategoryVo indexModel=new GoodsCategoryVo();
                //首页模块
                if(!CollectionUtils.isEmpty(goodsCategoryForFirstLevelVos))
                {
                    indexModel= Linq4j.asEnumerable(goodsCategoryForFirstLevelVos).where(new Predicate1<GoodsCategoryVo>() {
                        @Override
                        public boolean apply(GoodsCategoryVo goodsCategoryVo) {
                            return goodsCategoryVo.getCategoryName().equals("首页");
                        }
                    }).firstOrDefault();
                    if(indexModel!=null)
                        moduleDataRequest.setCategoryCode(indexModel.getCategoryCode());
                }
                categoryBannerVos = iModuleService.getCategoryBannerByCategoryCode(indexModel.getCategoryCode());
                moduleDataRequest.setCategoryCode(indexModel.getCategoryCode());
                moduleDataRequest.setPageindex(moduleDataRequest.getPageindex());//systemConfig.getDefaultPageIndex()
                moduleDataRequest.setPagesize(moduleDataRequest.getPagesize());//systemConfig.getDefaultPageSize()
                categoryGoodsVos = iModuleService.getRecommendGoodsGoodsArchivesByCategoryCode(moduleDataRequest);
            }
            else
            {
                categoryBannerVos = iModuleService.getCategoryBannerByCategoryCode(moduleDataRequest.getCategoryCode());
                moduleDataRequest.setPageindex(moduleDataRequest.getPageindex());
                moduleDataRequest.setPagesize(moduleDataRequest.getPagesize());//systemConfig.getDefaultPageSize()
                categoryGoodsVos = iModuleService.getRecommendGoodsGoodsArchivesByCategoryCode(moduleDataRequest);
                //获取分类
                goodsCategoryForSecondLevelVos= new GoodsCategoryUtil().switchModel(iGoodsCategoryService.getChildGoodsCategoryListByCategoryCode(moduleDataRequest.getCategoryCode()));
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        moduleDataResponse.setCategoryBannerVos(categoryBannerVos);
        moduleDataResponse.setCategoryGoodsVos(categoryGoodsVos);
        moduleDataResponse.setGoodsCategoryForFirstLevelVos(goodsCategoryForFirstLevelVos);
        moduleDataResponse.setGoodsCategoryForSecondLevelVos(goodsCategoryForSecondLevelVos);
        result.setData(moduleDataResponse);
        return result;
    }

    /**
     * 获取模块数据
     *
     * @param token             token令牌， 可为空
     * @param moduleDataRequest
     * @return json响应
     */
    @RequestMapping(value = "/getRecommendGoodsByPage", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult getRecommendGoodsByPage(@RequestHeader(value = "X-Long-Token", required = false) String token, @RequestBody ModuleDataRequest moduleDataRequest) {
        JsonResult result = new JsonResult();
        RecommendGoodsResponse recommendGoodsResponse = new RecommendGoodsResponse();
        List<CategoryGoodsVo> categoryGoodsVos = Lists.newArrayList();
        try {
            categoryGoodsVos = iModuleService.getRecommendGoodsGoodsArchivesByCategoryCode(moduleDataRequest);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        recommendGoodsResponse.setCategoryGoodsVos(categoryGoodsVos);
        result.setData(recommendGoodsResponse);
        return result;
    }
}
